using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BlogClient.Editor;
using BlogClient.Editor.XStandard;

namespace BlogClient
{
    public partial class MainForm : Form
    {
        IEditor _editor;
        string fileName = "";

        public MainForm()
        {
            InitializeComponent();

            /* TODO smart selection of editor based on config file */
            _editor = new XStandardEditor();
            editorPanel.SetEditor(_editor);

            CreateDefaultData();            
        }

        /// <summary>
        /// Creates the default data when you open the application
        /// and for a new blog. This is to help the user get started more
        /// easily.
        /// </summary>
        private void CreateDefaultData()
        {
            NewPost();
        }

        private BlogClient.BlogData.BlogData BlogData
        {
            get
            {
                return (BlogClient.BlogData.BlogData)livePostsBindingSource.DataSource;
            }
        }

        private void NewBlogPostLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            NewPost();
        }

        /// <summary>
        /// Adds a new post
        /// </summary>
        private void NewPost()
        {
            bool success = BlogData.LivePosts.GenerateNewPost();

            if (!success)
            {
                MessageBox.Show(
                    @"You have too many posts named ""New Post"", please remove one of them before adding a new post",
                    "Post Not Created",
                    MessageBoxButtons.OK);
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // We cannot assume that the editor will update the dataset automatically so this
            // command ensures we have the latest editor text in the dataset before we save it.
            editorPanel.DataBindings["Html"].WriteValue();


            BlogData.AcceptChanges();

            if (fileName == "")
            {
                DialogResult result = saveFileDialog1.ShowDialog();

                if (result == DialogResult.OK)
                {
                    fileName = saveFileDialog1.FileName;
                }
                else
                {
                    fileName = "";
                }
            }

            if (fileName != "")
            {
                BlogData.WriteXml(fileName, XmlWriteMode.WriteSchema);
            }

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            livePostsBindingSource.SuspendBinding();

            DialogResult result = openFileDialog1.ShowDialog();

            if (result == DialogResult.OK)
            {
                BlogData.Clear();
                BlogData.ReadXml(openFileDialog1.FileName, XmlReadMode.ReadSchema);
                fileName = openFileDialog1.FileName;
            }

            livePostsBindingSource.ResumeBinding();
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            BlogData.AcceptChanges();
        }
    }
}